﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;

public class Game4 : MonoBehaviour 
{
    public KaChe kaChe;
    public EndTip endTip;
    public GameObject curserTriggerPoint;
    public GameObject experiencePoint;
    public CameraPathBezierAnimator cameraPathAni;
    public GameObject gongRen;
    public GameObject gongRenMaoZi;
    public GameObject cameraRig;
    public MyCameraEffect cameraEffect;
    public AudioClip clip;
    public InputController leftController;
    public InputController rightController;
    void Awake()
    {
        PlayerPrefs.SetString("sceneName", "Scene4");
        cameraRig.GetComponent<SphereCollider>().enabled = true;
        //cameraRig.GetComponent<BoxCollider>().enabled = false;
        cameraEffect = cameraRig.transform.GetComponentInChildren<MyCameraEffect>();

    }
    void Start()
    {
        curserTriggerPoint.GetComponent<DistanceEvent>().OnNearEvent.AddListener(StartGame);
        experiencePoint.GetComponent<DistanceEvent>().OnNearEvent.AddListener(StartGame);
        kaChe.onAnimationOver += KaChe_onAnimationOver;
        cameraEffect.OnCancleBlack += CameraEffect_OnCancleBlack;
        CheckMode();
        SoundManager.instance.PlayBGMusic(clip);

    }

    private void CameraEffect_OnCancleBlack()
    {
        ShowTip();
    }

    private void KaChe_onAnimationOver()
    {
        GameOver();
    }

    

    /// <summary>
    /// 根据不同的模式开始不同的体验方式
    /// </summary>
    void CheckMode()
    {
        if (PlayerPrefs.GetString("mode") == "教育模式")
        {
            cameraPathAni.Play();
            experiencePoint.SetActive(false);
            MyControllerManager.instance.DisableController();

        }
        else
        {
            Debug.Log("开始体验模式");
            curserTriggerPoint.SetActive(false);
            gongRen.SetActive(false);
            gongRenMaoZi.SetActive(false);
        }
    }

    /// <summary>
    /// ---------------------------游戏开始
    /// </summary>
    void StartGame()
    {
        leftController.DisAbleTeleProt();
        rightController.DisAbleTeleProt();
        if (PlayerPrefs.GetString("mode") == "教育模式")
        {
            curserTriggerPoint.SetActive(false);
        }
        else
        {
            experiencePoint.SetActive(false);
        }
       
        kaChe.PlayAnimation();
    }

    /// <summary>
    /// ----------------------------游戏结束
    /// </summary>
    void GameOver()
    {
        cameraRig.GetComponent<SphereCollider>().enabled = true;
        if (PlayerPrefs.GetString("mode") == "体验模式")
        {
            cameraEffect.PlayDeadBlood();
        }else
        {
            Invoke("ShowTip",3f);
        }
        MyControllerManager.instance.EnableController();
        Invoke("Stop", 3f);
    }

    void Stop()
    {
        Rigidbody rig = cameraRig.GetComponent<Rigidbody>();
        rig.useGravity = true;
        rig.isKinematic = true;
        //leftController.DisAbleTeleProt();
        //rightController.DisAbleTeleProt();

    }

    private void ShowTip()
    {
        endTip.ShowTip(TipWords.game4_jiatongshanghai);
    }




}
